package com.shopping.dev.controller;

import com.shopping.dev.bo.CommonResultBO;
import com.shopping.dev.common.enums.YesOrNoEnum;
import com.shopping.dev.pojo.Carousel;
import com.shopping.dev.pojo.Category;
import com.shopping.dev.service.CarouselService;
import com.shopping.dev.service.CategoryService;
import com.shopping.dev.vo.CategoryVO;
import com.shopping.dev.vo.NewItemsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Api(value = "首页",tags = {"首页相关接口"})
@RequestMapping("/index")
@RestController
public class IndexController {
    @Autowired
    private CarouselService carouselService;
    @Autowired
    private CategoryService categoryService;

    @ApiOperation(value = "查询轮播图" ,notes = "查询所有轮播图，不包含不展示的",httpMethod = "GET")
    @GetMapping("/carousel")
    public CommonResultBO<Carousel> carousel(){
        List<Carousel> carousels = carouselService.queryAll(YesOrNoEnum.YES.getCode());
        return CommonResultBO.ok(carousels);
    }

    @ApiOperation(value = "获取一级分类" ,notes = "获取一级分类",httpMethod = "GET")
    @GetMapping("/cats")
    public CommonResultBO<Carousel> cats(){
        List<Category> catsRoot = categoryService.getCatsRoot();
        return CommonResultBO.ok(catsRoot);
    }

    @ApiOperation(value = "获取二级分类" ,notes = "获取二分类",httpMethod = "GET")
    @GetMapping("/subCat/{rootCatId}")
    public CommonResultBO<Carousel> subCat(
            @ApiParam(name = "rootCatId",value = "一级分类ID",required = true)
            @PathVariable("rootCatId") Integer rootCatId){
        if (rootCatId == null){
            return CommonResultBO.error("一级分类id不能为空");
        }
        List<CategoryVO> subCat = categoryService.getSubCat(rootCatId);
        return CommonResultBO.ok(subCat);
    }
    @ApiOperation(value = "查询每个分类下最新的6个商品" ,notes = "查询每个分类下最新的6个商品",httpMethod = "GET")
    @GetMapping("/sixNewItems/{rootCatId}")
    public CommonResultBO<Carousel> getSixNewItemsLazy(
            @ApiParam(name = "rootCatId",value = "一级分类ID",required = true)
            @PathVariable("rootCatId") Integer rootCatId){
        if (rootCatId == null){
            return CommonResultBO.error("一级分类id不能为空");
        }
        List<NewItemsVO> itemsVOS = categoryService.getSixNewItemsLazy(rootCatId);
        return CommonResultBO.ok(itemsVOS);
    }


}
